<template>
	<view class="content" v-if="gasstation">
		<view class="wid90 mar">
			<!-- <view class="hei20"></view> -->
			<view class="wid100 dis disAl hei80">
				<image src="../../static/jiayou.png" style="width: 26rpx;height: 26rpx;"></image>
				<view class="foSi30 mar-left20 fowe600" @click="tiaozhuan('/pages/merchant/merchant?id='+jiaId)">
					{{gasstation.name}}
					<uni-icons type="right" size="30rpx"></uni-icons>
				</view>
			</view>
			<swiper v-if="gasstation.img" class="wid100 borRad10" style="height: 273rpx;" circular :autoplay='true'>
				<swiper-item v-for="(item,index) in gasstation.img.split(',')" :key="index">
					<image :src="url+item" class="wh100"></image>
				</swiper-item>
			</swiper>
			<view class="wid100 mar-top30 borRad20 bacFFF foSi25" style="height: 219rpx;">
				<view style="height: 40rpx;"></view>
				<view class="wid95 mar dis">
					<view class="textCen" style="width: 20%;" @click="tiaozhuan('/pages/store/store')">
						<image src="../../static/guanli.png" style="width: 84rpx;height: 84rpx;"></image>
						<view class="mar-top10">积分商城</view>
					</view>
					<view class="textCen" style="width: 20%;" @click="tiaozhuan('/pages/getACoupon/getACoupon')">
						<image src="../../static/biangeng.png" style="width: 84rpx;height: 84rpx;"></image>
						<view class="mar-top10">领券中心</view>
					</view>
					<view class="textCen" style="width: 20%;" @click="tiaozhuan('/pages/referrer/referrer')">
						<image src="../../static/zhongduan.png" style="width: 84rpx;height: 84rpx;"></image>
						<view class="mar-top10">推荐有礼</view>
					</view>
					<view class="textCen" style="width: 20%;" @click="tiaozhuan('/pages/index/activity')">
						<image src="../../static/huodong.png" style="width: 84rpx;height: 84rpx;"></image>
						<view class="mar-top10">充值活动</view>
					</view>
					<view class="textCen" style="width: 20%;" @click="tiaozhuan('/pages/giftBag/giftBag')">
						<image src="../../static/shouyi.png" style="width: 84rpx;height: 84rpx;"></image>
						<view class="mar-top10">新人礼包</view>
					</view>
				</view>
			</view>
			<view class="hei20"></view>
		</view>
		<view class="wid90 mar bacFFF borRad20 mar-top20" style="height: 300rpx;">
			<view class="hei20"></view>
			<view class="wid90 mar hei60 dis disAl disJuB">
				<view class="foSi35 fowe600">今日油价</view>
				<view class="foSi20" style="color: #878787;">数据统计截至{{date}}</view>
			</view>
			<view class="wid90 mar mar-top20" style="background-color: #F8F9FD;">
				<view class="wid100 hei80 dis" style="border-bottom: 1rpx solid #fff;color: #373737;">
					<view class="dis disAl disJuC foSi25 fowe600"
						style="color: #035DFF;width: 29%;border-right: 1rpx solid #fff;">油品型号</view>
					<view class="dis disAl disJuC foSi25" style="width: 15%;border-right: 1rpx solid #fff;">92#</view>
					<view class="dis disAl disJuC foSi25" style="width: 15%;border-right: 1rpx solid #fff;">95#</view>
					<view class="dis disAl disJuC foSi25" style="width: 20%;border-right: 1rpx solid #fff;">0#柴油</view>
					<view class="dis disAl disJuC foSi25" style="width: 21%;">-35#柴油</view>
				</view>
				<view class="wid100 hei80 dis" style="border-bottom: 1rpx solid #fff;color: #373737;">
					<view class="dis disAl disJuC foSi25 fowe600"
						style="color: #035DFF;width: 29%;border-right: 1rpx solid #fff;">油价(元/升)</view>
					<view class="dis disAl disJuC foSi25" style="width: 15%;border-right: 1rpx solid #fff;">
						{{gasstation.ninety_two_oil}}</view>
					<view class="dis disAl disJuC foSi25" style="width: 15%;border-right: 1rpx solid #fff;">
						{{gasstation.ninety_five_oil}}</view>
					<view class="dis disAl disJuC foSi25" style="width: 20%;border-right: 1rpx solid #fff;">
						{{gasstation.zero_oil}}</view>
					<view class="dis disAl disJuC foSi25" style="width: 21%;">{{gasstation.negative_thirty_five_oil}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				gasstation: '',
				jiaId: '',
				date: '', //现在时间
				timer: null
			}
		},
		onLoad(option) {
			var that = this
			that.getTx()
			// alert(option.parent_id)
			if(option.parent_id){
				uni.setStorageSync('parent_id',option.parent_id)
			}else{
				uni.setStorageSync('parent_id',0)
			}
			that.date = that.dateFormat(new Date())
			that.timer = setInterval(function() {
				const now = new Date();
				if (now.getSeconds() == 1) {
					that.date = that.dateFormat(now)
					that.$forceUpdate()
					console.log(that.date)
				}
			}, 1000);
		},
		onShow() {
			var jiaId = uni.getStorageSync('jiaId')
			if (jiaId) {
				this.jiaId = jiaId
				this.getList()
			} else {
				
				this.getLocation()
				// this.tiaozhuan('/pages/merchant/merchant')
			}
		},
		methods: {
			
				getLocation() {
					let that = this
					// 获取用户是否开启 授权获取当前的地理位置、速度的权限。
					uni.getSetting({
						success(res) {
							console.log(res)
							// 如果没有授权
							if (!res.authSetting['scope.userLocation']) {
								// 则拉起授权窗口
								uni.authorize({
									scope: 'scope.userLocation',
									success() {
										//点击允许后--就一直会进入成功授权的回调 就可以使用获取的方法了
										uni.getLocation({
											type: 'wgs84',
											success: function(res) {
												uni.setStorageSync('location_y', res.longitude.toFixed(6))
												uni.setStorageSync('location_x', res.latitude.toFixed(6))
												that.getGasstationList()
											},
											fail(error) {
												console.log('失败', error)
											}
										})
									},
									fail(error) {
										//点击了拒绝授权后--就一直会进入失败回调函数--此时就可以在这里重新拉起授权窗口
										console.log('拒绝授权', error)
										that.getGasstationList()
									}
								})
							} else {
								// 有权限则直接获取
								uni.getLocation({
									type: 'wgs84',
									success: function(res) {
										uni.setStorageSync('location_y', res.longitude.toFixed(6))
										uni.setStorageSync('location_x', res.latitude.toFixed(6))
										that.getGasstationList()
									},
									fail(error) {
										uni.showToast({
											title: '请勿频繁调用！',
											icon: 'none',
										})
										console.log('失败', error)
										that.getGasstationList()
									}
								})
							}
						}
					})
				},
				getGasstationList() {
					var that = this;
					uni.request({
						url: that.global.url + '/api/qianyu_petrol/gasstation_list',
						method: "POST",
						header: {
							'site-id': that.global.site_id, //自定义请求头信息
						},
						data: {
							location_y: uni.getStorageSync('location_y')?uni.getStorageSync('location_y'):'104.480609',
							location_x: uni.getStorageSync('location_x')?uni.getStorageSync('location_x'):'36.305564',
							page: that.page,
							limit: 10,
						},
						success(res) {
							let result = res.data
							console.log(result)
							if (result.code == 1) {
								if(uni.getStorageSync('jiaId')){
									that.last_page = result.data.last_page
									that.list = that.list.concat(result.data.data)
								}else{
									uni.setStorageSync('jiaId', result.data.data[0].id)
									// uni.switchTab({
									// 	url: '/pages/index/index'
									// })
									that.getList()
									// setTimeout(() => {
									// 	this.$router.go(0)
									// }, 500)
								}
							}
						},
						complete() {
							uni.hideLoading()
						}
					})
				},
			getTx() {
				var that = this
				uni.request({
					url: this.global.url + '/api/qianyu_petrol/gasstation_template_message',
					method: "POST",
					header: {
						'site-id': that.global.site_id,
					},
					data: {},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							var tzId = result.data[0]
							uni.setStorageSync('tzId', tzId)
						}
					}
				})
			},
			dateFormat(time) {
				let date = new Date(time);
				let year = date.getFullYear();
				// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				return year + "年" + month + "月" + day + "日 " + hours + ":" + minutes;
				// return year + "-" + month + "-" + day;
			},
			getList() {
				var that = this;
				uni.request({
					url: that.global.url + '/api/qianyu_petrol/gasstation_index',
					method: "POST",
					header: {
						'site-id': that.global.site_id, //自定义请求头信息
					},
					data: {
						id: that.jiaId
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							that.gasstation = result.data
						}
					}
				})
			},
			tiaozhuan(url) {
				var that = this
				var userinfo = uni.getStorageSync('userinfo')
				if(!userinfo && url == '/pages/index/activity'){
					uni.switchTab({
						url: '/pages/wode/wode'
					})
				}else{
					uni.navigateTo({
						url: url
					})
				}
			}
		}
	}
</script>

<style>
	.content {
		background: linear-gradient(#E4F7FD 20%, #F8F9FD 80%);
		min-height: 100vh;
	}
</style>